package com.pshcp.service;

import com.framework.base.PageList;
import com.framework.tool.CommonTool;
import com.framework.util.SystemUtil;
import com.github.pagehelper.PageHelper;
import com.pshcp.common.enums.CommonStatusEnum;
import com.pshcp.domain.dto.DeptGroupDTO;
import com.pshcp.domain.model.DeptGroup;
import com.pshcp.mapper.DeptGroupMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 科室分组信息表 服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2023-02-07
 */
@Service
public class DeptGroupService {

    @Autowired
    private DeptGroupMapper deptGroupMapper;

    public PageList<DeptGroup> list(DeptGroupDTO deptGroupDTO){
        if(deptGroupDTO.getPageNum() != null && deptGroupDTO.getPageSize() != null){
            PageHelper.startPage(deptGroupDTO.getPageNum(), deptGroupDTO.getPageSize());
        }
        Example example = new Example(DeptGroup.class);
        Example.Criteria criteria = example.createCriteria();
        if(deptGroupDTO.getStatus() != null){
            criteria.andEqualTo("status", deptGroupDTO.getStatus());
        }else{
            criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        }
        example.setOrderByClause(" created_time desc");
        List<DeptGroup> list = deptGroupMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public List<DeptGroup> select(DeptGroup deptGroup){
        return deptGroupMapper.select(deptGroup);
    }

    public int insert(DeptGroup deptGroup){
        deptGroup.setId(SystemUtil.getId());
        deptGroup.setStatus(CommonStatusEnum.INIT.getCode());
        deptGroup.setCreatedTime(new Date());
        return deptGroupMapper.insert(deptGroup);
    }

    public int update(DeptGroup deptGroup){
        deptGroup.setUpdateTime(new Date());
        return deptGroupMapper.updateByPrimaryKeySelective(deptGroup);
    }

    public DeptGroup findById(Long id){
        return deptGroupMapper.selectByPrimaryKey(id);
    }

    public List<DeptGroup> findByIds(List<Long> ids){
        Example example = new Example(DeptGroup.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return deptGroupMapper.selectByExample(example);
    }

    public int delById(Long id){
        Example example = new Example(DeptGroup.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        DeptGroup deptGroup = new DeptGroup();
        deptGroup.setStatus(CommonStatusEnum.DEL.getCode());
        return deptGroupMapper.updateByExampleSelective(deptGroup, example);
    }

}
